package com.kgc.sportscircle.impl;

import com.kgc.sportscircle.pojo.Sportsuser;
import com.kgc.sportscircle.dao.SportsuserMapper;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.kgc.sportscircle.utils.Page;
import javax.annotation.Resource;
import com.kgc.sportscircle.vo.SportsuserParam;
import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sportscircle.service.SportsuserService;
import org.springframework.transaction.annotation.Transactional;

/**
* 描述： 服务实现层接口
* @author 杨海文
* @date 2019/07/22
*/
@Service(timeout = 50000,retries = 2,version = "1.11",interfaceClass=SportsuserService.class)
public class SportsuserServiceImpl implements SportsuserService{
	@Resource
	private SportsuserMapper sportsuserMapper;
 	
 	/**
    * 描述：根据Id获取Sportsuser对象
    * @param id
    */
    public Sportsuser findById(Serializable id){
    	return sportsuserMapper.findById(id);
    }
    /**
    * 描述：获取所有的Sportsuser对象
    */
    public List<Sportsuser> findAll(){
    	return sportsuserMapper.findAll();
    }

	/**
    * 描述：新增保存Sportsuser对象
    */
    public int save(Sportsuser sportsuser){
    	return  sportsuserMapper.save(sportsuser);
    }

	/**
    * 描述：根据Id删除Sportsuser对象
    * @param id
    */
    public int delete(Serializable id){
    	return  sportsuserMapper.delete(id);
    }
	
	/**
    * 描述：更新Sportsuser对象
    */
    public int update(Sportsuser sportsuser){
    	return  sportsuserMapper.update(sportsuser);
    }
    
    /**
     * 根据不确定的条件，动态查询
     */
    public List<Sportsuser> query(SportsuserParam param){
    	return  sportsuserMapper.query(param);
    }
    
    /**
     * 根据不确定的条件，动态分页查询
     */
    public Page queryByPage(SportsuserParam param){
    	Page page = new Page();
    	if(param.isPage()){
			int totalCount = sportsuserMapper.getCount(param);
			int totalPageCount = (totalCount-1)/page.getPageSize()+1;
			if(param.getPageIndex()<1){
				param.setPageIndex(1);
			}else if(param.getPageIndex()>totalPageCount){
				param.setPageIndex(totalPageCount);
			}
			List list = sportsuserMapper.query(param);

			page.setTotalCount(totalCount);
			page.setTotalPageCount(totalPageCount);
    		page.setPageNo(param.getPageIndex());
    		page.setPageSize(param.getSize());
			page.setList(list);
		}
    	return page;
    	
    }

	@Transactional
	public int register(Sportsuser sportsuser) {
		String mobile = sportsuser.getMobile();
		//查询手机号是否有存在
		Sportsuser su = sportsuserMapper.selectUserByMobile(mobile);
		if (su!=null){
			return -7;
		}
		int msg = sportsuserMapper.save(sportsuser);
		return msg;
	}


	public int verifyMobile(String mobile) {
		Sportsuser su = sportsuserMapper.selectUserByMobile(mobile);
		if (su != null) {
			return -7;
		}
		return 0;
	}

	public Sportsuser selectUserByMobile(String mobile) {
		return sportsuserMapper.selectUserByMobile(mobile);
	}
}
